<?php


namespace app\system\controller;

class CardApp extends AdminBase
{
	public $project = [];
	public function __construct()
	{
		parent::__construct();
		if (!defined("no_authentication")) {
			$app = \think\Db::name("app")->where(["id" => $this->input["appid"]])->find();
			if (empty($app)) {
				$this->response(\app\system\model\Code::fail, "appid不存在");
			}
			$this->project = $app;
			if ($this->project["type"] !== 0) {
				$this->response(\app\system\model\Code::fail, "该应用ID的应用类型不支持请求当前模块接口");
			}
		}
	}
	public function get_card_list()
	{
		$options = $this->request->get();
		$options["limit"] = $this->page_size;
		$options["page"] = $this->page_no;
		$result = \app\system\service\CardAppService::get_card_list($options);
		$this->response($result["code"], "获取成功", $result["data"], ["count", $result["count"]], ["pages", $result["pages"]]);
	}
	public function get_card_info()
	{
		$options = $this->request->get();
		$result = \app\system\service\CardAppService::get_card_info($options);
		if ($result["code"] == \app\system\model\Code::success) {
			$this->response($result["code"], "获取成功", $result["data"]);
		} else {
			$this->response($result["code"], $result["msg"]);
		}
	}
	public function add_card()
	{
		$options = $this->request->post();
		if (empty($options["add_mode"])) {
			$validate = \think\Validate::make(["appid" => "require", "num" => "require|number|>=:1", "time_type" => "require", "time_value" => "require"], ["appid.require" => "appid 不能为空", "num.require" => "请输入数量", "time_type.require" => "请选择时间类型", "time_value.require" => "请输入时长"]);
		} else {
			if ($options["add_mode"] == 1) {
				$validate = \think\Validate::make(["appid" => "require", "custom_list" => "require|array", "time_type" => "require", "time_value" => "require"], ["appid.require" => "appid 不能为空", "custom_list.require" => "请输入卡密", "time_type.require" => "请选择时间类型", "time_value.require" => "请输入时长"]);
			} elseif ($options["add_mode"] == 2) {
				$validate = \think\Validate::make(["appid" => "require", "importCardTableData" => "require|array"], ["appid.require" => "appid 不能为空", "importCardTableData.require" => "请导入卡密"]);
			}
		}
		if (!$validate->check($options)) {
			$this->response(\app\system\model\Code::fail, $validate->getError());
		}
		$result = \app\system\service\CardAppService::add_card($options);
		if ($result["code"] == \app\system\model\Code::success) {
			$this->response($result["code"], $result["msg"], $result["data"]);
		} else {
			$this->response($result["code"], $result["msg"]);
		}
	}
	public function edit_card()
	{
		$options = $this->request->post();
		$validate = \think\Validate::make(["appid" => "require", "id" => "require"], ["appid.require" => "appid 不能为空", "id.require" => "id 不能为空"]);
		if (!$validate->check($options)) {
			$this->response(\app\system\model\Code::fail, $validate->getError());
		}
		$result = \app\system\service\CardAppService::edit_card($options);
		if ($result["code"] == \app\system\model\Code::success) {
			$this->response($result["code"], "编辑成功");
		} else {
			$this->response($result["code"], $result["msg"]);
		}
	}
	public function del_card()
	{
		$options = $this->request->post();
		$result = \app\system\service\CardAppService::del_card($options);
		if ($result["code"] == \app\system\model\Code::success) {
			$this->response($result["code"], "删除成功");
		} else {
			$this->response($result["code"], $result["msg"]);
		}
	}
	public function del_card_batch()
	{
		$options = $this->request->post();
		$result = \app\system\service\CardAppService::del_card_batch($options);
		if ($result["code"] == \app\system\model\Code::success) {
			$this->response($result["code"], "删除成功");
		} else {
			$this->response($result["code"], $result["msg"]);
		}
	}
	public function get_card_batch_list()
	{
		$options = $this->request->get();
		$options["limit"] = $this->page_size;
		$options["page"] = $this->page_no;
		$result = \app\system\service\CardAppService::get_card_batch_list($options);
		$this->response($result["code"], "获取成功", $result["data"], ["count", $result["count"]], ["pages", $result["pages"]], ["all_card_data", $result["all_card_data"]]);
	}
	public function card_disable()
	{
		$options = $this->request->post();
		$result = \app\system\service\CardAppService::card_disable($options);
		if ($result["code"] == \app\system\model\Code::success) {
			$this->response($result["code"], "设置成功");
		} else {
			$this->response($result["code"], $result["msg"]);
		}
	}
	public function export_card()
	{
		// 关闭错误显示，防止 PHPExcel 的 Deprecated 警告污染 JSON 响应
		error_reporting(E_ALL & ~E_DEPRECATED & ~E_NOTICE);

		$options = $this->request->post();
		$validate = \think\Validate::make(["appid" => "require", "batch_id" => "require", "export_file_type" => "require|in:txt,excel"], ["appid.require" => "appid 不能为空", "batch_id.require" => "batch_id 不能为空", "export_file_type.require" => "export_file_type 不能为空"]);
		if (!$validate->check($options)) {
			$this->response(\app\system\model\Code::fail, $validate->getError());
		}
		$options["limit"] = $this->page_size_max;
		$options["page"] = 1;
		$result = \app\system\service\CardAppService::export_card_user_table($options);
		if ($result["code"] == \app\system\model\Code::success) {
			$this->response($result["code"], $result["msg"], $result["data"]);
		} else {
			$this->response($result["code"], $result["msg"]);
		}
	}
	public function get_card_account_finance_log()
	{
		$options = $this->request->get();
		$options["limit"] = $this->page_size;
		$options["page"] = $this->page_no;
		$result = \app\system\service\CardAppService::get_card_account_finance_log($options);
		$this->response($result["code"], "获取成功", $result["data"], ["count", $result["count"]], ["pages", $result["pages"]]);
	}
	public function get_card_unbinding_log()
	{
		$options = $this->request->get();
		$options["limit"] = $this->page_size;
		$options["page"] = $this->page_no;
		$result = \app\system\service\CardAppService::get_card_unbinding_log($options);
		$this->response($result["code"], "获取成功", $result["data"], ["count", $result["count"]], ["pages", $result["pages"]]);
	}
	public function condition_del_card()
	{
		$options = $this->request->post();
		$result = \app\system\service\CardAppService::condition_del_card($options);
		if ($result["code"] == \app\system\model\Code::success) {
			$this->response($result["code"], $result["msg"]);
		} else {
			$this->response($result["code"], $result["msg"]);
		}
	}
	public function condition_unbinding_card()
	{
		$options = $this->request->post();
		$result = \app\system\service\CardAppService::condition_unbinding_card($options);
		if ($result["code"] == \app\system\model\Code::success) {
			$this->response($result["code"], $result["msg"]);
		} else {
			$this->response($result["code"], $result["msg"]);
		}
	}
	public function condition_increase_duration_card()
	{
		$options = $this->request->post();
		$validate = \think\Validate::make(["appid" => "require", "value" => "require"], ["appid.require" => "appid 不能为空", "value.require" => "时长数量 不能为空"]);
		if (!$validate->check($options)) {
			$this->response(\app\system\model\Code::fail, $validate->getError());
		}
		$result = \app\system\service\CardAppService::condition_increase_duration_card($options);
		if ($result["code"] == \app\system\model\Code::success) {
			$this->response($result["code"], $result["msg"]);
		} else {
			$this->response($result["code"], $result["msg"]);
		}
	}
	public function condition_increase_point_card()
	{
		$options = $this->request->post();
		$validate = \think\Validate::make(["appid" => "require", "value" => "require"], ["appid.require" => "appid 不能为空", "value.require" => "积分数量 不能为空"]);
		if (!$validate->check($options)) {
			$this->response(\app\system\model\Code::fail, $validate->getError());
		}
		$result = \app\system\service\CardAppService::condition_increase_point_card($options);
		if ($result["code"] == \app\system\model\Code::success) {
			$this->response($result["code"], $result["msg"]);
		} else {
			$this->response($result["code"], $result["msg"]);
		}
	}
	public function condition_export_card()
	{
		// 关闭错误显示，防止 PHPExcel 的 Deprecated 警告污染 JSON 响应
		error_reporting(E_ALL & ~E_DEPRECATED & ~E_NOTICE);

		$options = $this->request->post();
		$validate = \think\Validate::make(["appid" => "require", "export_file_type" => "require|in:txt,excel"], ["appid.require" => "appid 不能为空", "export_file_type.require" => "export_file_type 不能为空"]);
		if (!$validate->check($options)) {
			$this->response(\app\system\model\Code::fail, $validate->getError());
		}
		$options["limit"] = $this->page_size_max;
		$options["page"] = 1;
		$result = \app\system\service\CardAppService::export_card_user_table($options);
		if ($result["code"] == \app\system\model\Code::success) {
			$this->response($result["code"], $result["msg"], $result["data"]);
		} else {
			$this->response($result["code"], $result["msg"]);
		}
	}
	public function condition_force_offline_card()
	{
		$options = $this->request->post();
		$result = \app\system\service\CardAppService::condition_force_offline_card($options);
		if ($result["code"] == \app\system\model\Code::success) {
			$this->response($result["code"], $result["msg"]);
		} else {
			$this->response($result["code"], $result["msg"]);
		}
	}
	public function condition_disable_card()
	{
		$options = $this->request->post();
		$validate = \think\Validate::make(["appid" => "require", "value" => "require"], ["appid.require" => "appid 不能为空", "value.require" => "value 不能为空"]);
		if (!$validate->check($options)) {
			$this->response(\app\system\model\Code::fail, $validate->getError());
		}
		$result = \app\system\service\CardAppService::condition_disable_card($options);
		if ($result["code"] == \app\system\model\Code::success) {
			$this->response($result["code"], $result["msg"]);
		} else {
			$this->response($result["code"], $result["msg"]);
		}
	}
	public function card_force_offline()
	{
		$options = $this->request->post();
		$result = \app\system\service\CardAppService::card_force_offline($options);
		if ($result["code"] == \app\system\model\Code::success) {
			$this->response($result["code"], "设置成功");
		} else {
			$this->response($result["code"], $result["msg"]);
		}
	}
	public function card_force_offline_machine()
	{
		$options = $this->request->post();
		$result = \app\system\service\CardAppService::card_force_offline_machine($options);
		if ($result["code"] == \app\system\model\Code::success) {
			$this->response($result["code"], "设置成功");
		} else {
			$this->response($result["code"], $result["msg"]);
		}
	}
	public function revise_batch_remarks()
	{
		$options = $this->request->post();
		$validate = \think\Validate::make(["appid" => "require", "value" => "require"], ["appid.require" => "appid 不能为空", "value.require" => "备注内容 不能为空"]);
		if (!$validate->check($options)) {
			$this->response(\app\system\model\Code::fail, $validate->getError());
		}
		$result = \app\system\service\CardAppService::revise_batch_remarks($options);
		if ($result["code"] == \app\system\model\Code::success) {
			$this->response($result["code"], "修改成功");
		} else {
			$this->response($result["code"], $result["msg"]);
		}
	}
	public function get_login_machine_list()
	{
		$options = $this->request->get();
		$validate = \think\Validate::make(["appid" => "require", "id" => "require"], ["appid.require" => "appid 不能为空", "id.require" => "id 不能为空"]);
		if (!$validate->check($options)) {
			$this->response(\app\system\model\Code::fail, $validate->getError());
		}
		$options["limit"] = $this->page_size;
		$options["page"] = $this->page_no;
		$result = \app\system\service\CardAppService::get_login_machine_list($options);
		$this->response($result["code"], "获取成功", $result["data"], ["count", $result["count"]], ["pages", $result["pages"]]);
	}
	public function del_login_machine()
	{
		$options = $this->request->post();
		$result = \app\system\service\CardAppService::del_login_machine($options);
		if ($result["code"] == \app\system\model\Code::success) {
			$this->response($result["code"], "删除成功");
		} else {
			$this->response($result["code"], $result["msg"]);
		}
	}
}